package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/add-one-row-to-tree/description/
 * @create 2025/9/12 15:21
 * @since JDK17
 */

public class Solution22 {
    int depth, val;
    void f(TreeNode root, int cur) {
        if (cur == depth - 1) { // 时间差不多了
            root.left = new TreeNode(val, root.left, null);
            root.right = new TreeNode(val, null, root.right);
        }
        f(root.left, cur + 1);
        f(root.right, cur + 1);
    }

    public TreeNode addOneRow(TreeNode root, int val, int depth) {
        if (depth == 1) {
            return new TreeNode(val, root, null);
        }
        this.depth = depth;
        this.val = val;
        f(root, 1);
        return root;
    }
}
